package com.biocar.controller;

import com.biocar.ResBean;
import com.biocar.bean.Charge;
import com.biocar.service.impl.ChargeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.NoSuchElementException;

/**
 * @author quxiang
 * @date 2021/11/24 16:28
 * 费用接口
 */

@RestController
@RequestMapping("/charge")
public class ChargeController {
    @Autowired
    ChargeServiceImpl chargeService;

    /**
     * 根据id查询费用
     *
     * @param id
     * @return {
     * "code": 20000,
     * "message": "success",
     * "data": {
     * "id": 1,
     * "number": "1",
     * "costDate": "1637164800000",
     * "account": "刘宇农行",
     * "money": 200.0,
     * "moneyType": "RMB",
     * "bill": "",
     * "saleman": "邓菲",
     * "dept": "国外运营部",
     * "tips": "国外运费",
     * "creator": "邓菲",
     * "createDate": "1637164800000",
     * "status": 1
     * }
     * }
     */
    @GetMapping("/query/{id}")
    public ResBean<Charge> queryCharge(@PathVariable Integer id) {
        try {
            Charge charge = chargeService.selectById(id);
            return ResBean.successWithObj(charge);
        } catch (NoSuchElementException e) {
            return ResBean.failWithMsg("没有找到指定的费用");
        }
    }

    /**
     * 删除费用
     *
     * @param id
     * @return
     */
    @PostMapping("/delete")
    public ResBean<Void> deleteById(@RequestParam int id) {
        try {
            chargeService.deleteById(id);
            return ResBean.success();
        } catch (NoSuchElementException e) {
            return ResBean.failWithMsg("不存在");
        }
    }

    /**
     * 插入费用
     *
     * @param charge
     * @return
     */
    @PostMapping("/insert")
    public ResBean<Void> insert(@RequestBody Charge charge) {
        try {
            chargeService.insert(charge);
            return ResBean.success();
        } catch (NoSuchElementException e) {
            return ResBean.failWithMsg("插入失败");
        }
    }

    /**
     * 更新费用单
     *
     * @param charge
     * @return
     */
    @PostMapping("/update")
    public ResBean<Void> update(@RequestBody Charge charge) {
        try {
            chargeService.update(charge);
            return ResBean.success();
        } catch (NoSuchElementException e) {
            return ResBean.failWithMsg("修改失败");
        }
    }

}
